package com.qianke.common.springmvc.interceptor;

import com.alibaba.fastjson.JSONObject;
import com.qianke.common.FastJson;
import com.qianke.common.WebHelper;
import org.apache.commons.lang3.RandomStringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class RequestLogMdcInterceptor extends HandlerInterceptorAdapter {
    public static final Logger logger = LoggerFactory.getLogger(RequestLogMdcInterceptor.class);

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String unqId = RandomStringUtils.randomNumeric(16);
        MDC.put("req.id", unqId);
        MDC.put("req.uri", request.getRequestURI());
        if (request.getParameter("imei") != null) {
            MDC.put("req.imei", request.getParameter("imei"));
        }
        MDC.put("req.beginTime", System.currentTimeMillis() + "");

        logger.info("req method:{},  param : {}", request.getMethod(), JSONObject.toJSON(WebHelper.getRequestParam(request)));
        return super.preHandle(request, response, handler);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

        logger.info("请求：{}, 耗时：{} ms", MDC.get("req.uri"), System.currentTimeMillis() - Long.parseLong(MDC.get("req.beginTime")));

        MDC.remove("req.id");
        MDC.remove("req.uri");
        MDC.remove("req.imei");
        MDC.remove("req.beginTime");

        super.afterCompletion(request, response, handler, ex);
    }
}
